﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Transactions;
using System.Web;
using System.Web.Mvc;
using System.Web.Security;
using DotNetOpenAuth.AspNet;
using Microsoft.Web.WebPages.OAuth;
using WebMatrix.WebData;
using SYMetro.DataObjects;
using SYMetro.Web.MVC.Filters;

namespace SYMetro.Web.MVC.Controllers
{
    [Authorize]
    [ExtRequestFilter]
    public class AccountController : Controller
    {

        [AllowAnonymous]
        public ActionResult Login(string returnUrl)
        {
            ViewBag.ReturnUrl = returnUrl;
            return View();
        }

        [HttpPost]        
        [AllowAnonymous]        
        public JsonResult Login(UserDataObject model, string returnUrl)
        {
            if (ModelState.IsValid)
            {
                if (Membership.ValidateUser(model.UserName, model.Password))
                {
                    FormsAuthentication.SetAuthCookie(model.UserName, model.RememberMe);

                    return this.Json(new
                    {
                        success = true,
                        msg = "登录成功!"
                    });
                }
                else
                {
                    return this.Json(new
                    {
                        success = false,
                        msg = "您输入的用户名或密码错误!"
                    });                    
                }
            }
            else
            {
                return this.Json(new
                {
                    success = false,
                    msg = "ModelState验证无效!"
                });
            }
        }

        [HttpPost]        
        public ActionResult LogOff()
        {
            FormsAuthentication.SignOut();
            Session["UserID"] = null;
            return RedirectToAction("Index", "Home");            
        }

        [AllowAnonymous]        
        public JsonResult GetUser(string id)
        {
            if (!string.IsNullOrEmpty(id))
            {
                var u = new UserDataObject()
                {
                    ID = id,
                    UserName = "User1",
                    LoweredUserName = "user1"
                };
                return this.Json(u, JsonRequestBehavior.AllowGet);
            }
            else
            {
                UserDataObjectList list = new UserDataObjectList();
                list.Add(new UserDataObject()
                {
                    ID = Guid.NewGuid().ToString(),
                    UserName = "User1"
                });
                list.Add(new UserDataObject()
                {
                    ID = Guid.NewGuid().ToString(),
                    UserName = "User2"
                });
                return this.Json(list, JsonRequestBehavior.AllowGet);
            }
        }

        [AllowAnonymous]
        [HttpPost]
        [JsonModelPost(JsonModelPostAttribute.ParameterType.Multiple)]
        public JsonResult CreateUser(List<UserDataObject> users)
        {
            ProcessUsers(users);

            return this.Json(new { success = true, user = users }, JsonRequestBehavior.AllowGet);
        }

        private List<UserDataObject> ProcessUsers(List<UserDataObject> users)
        {
            int i = 1;
            foreach (var user in users)
            {
                user.ID = Guid.Empty.ToString();
                user.UserName = "Server Name" + i++;
            }
            return users;
        }

        [AllowAnonymous]
        [HttpPost]
        [JsonModelPost(JsonModelPostAttribute.ParameterType.Single)]
        public JsonResult CreateUser(UserDataObject user)
        {
            var list = new List<UserDataObject>(new UserDataObject[] { user });
            ProcessUsers(list);
            return this.Json(new { success = true, user = list[0] }, JsonRequestBehavior.AllowGet);
        }

        [AllowAnonymous]
        [HttpPost]
        [JsonModelPost(JsonModelPostAttribute.ParameterType.Single)]
        public JsonResult UpdateUser(UserDataObject user)
        {
            user.CreateDate = DateTime.Now;
            user.Email = "2222@fda.com";
            user.ID = Guid.NewGuid().ToString();
            return this.Json(new { success = true, user = user }, JsonRequestBehavior.AllowGet);
        }

        [AllowAnonymous]
        [HttpPost]
        [JsonModelPost(JsonModelPostAttribute.ParameterType.Multiple)]
        public JsonResult UpdateUser(List<UserDataObject> users)
        {
            foreach (var user in users)
            {
                user.CreateDate = DateTime.Now;
            }

            return this.Json(new { success = true, user = users }, JsonRequestBehavior.AllowGet);
        }

        [AllowAnonymous]
        [HttpPost]
        [JsonModelPost(JsonModelPostAttribute.ParameterType.Single)]
        public JsonResult DeleteUser(UserDataObject user)
        {
            return this.Json(new { success = false, Message = "Test Error" }, JsonRequestBehavior.AllowGet);
        }

        [AllowAnonymous]
        [HttpPost]
        [JsonModelPost(JsonModelPostAttribute.ParameterType.Multiple)]
        public JsonResult DeleteUser(List<UserDataObject> user)
        {
            return this.Json(new { success = false, Message = "Test Error" }, JsonRequestBehavior.AllowGet);
        }

        #region Helpers
        private ActionResult RedirectToLocal(string returnUrl)
        {
            if (Url.IsLocalUrl(returnUrl))
            {
                return Redirect(returnUrl);
            }
            else
            {
                return RedirectToAction("Index", "Home");
            }
        }
 
        #endregion
    }
}
